package io.renren.controller;

import io.renren.constant.AppConstant;
import io.renren.po.AD;
import io.renren.vo.RespModel;
import io.renren.service.AdService;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/AD")
@Api(tags = "查询广告"  )
@Component
public class AdResourceController {

    private static final Logger logger = LoggerFactory.getLogger(AdResourceController.class);

    @Autowired
    private AdService adService;

    @PostMapping(value = "/select", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查询一条广告", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "OK", response = RespModel.class),
            @ApiResponse(code = 500, message = "服务端异常", response = RespModel.class)})
    @com.cxytiandi.encrypt.springboot.annotation.Decrypt
    @com.cxytiandi.encrypt.springboot.annotation.Encrypt
    public RespModel<AD> select(@ApiParam(value = "入参id查询(1新闻内容公告页2新闻内容内容页3出行服务页4交警在线页)") AD ad) throws Exception {
        AD result = adService.select(ad);
        RespModel<AD> resp = new RespModel(AppConstant.STATU_SUCCESS.getName(), AppConstant.STATU_SUCCESS.getValue(), "", result);
        return resp;
    }

    @GetMapping(value = "/selectContent/{id}", produces = MediaType.TEXT_HTML_VALUE)
    @ApiOperation(value = "通过广告ID查询h5的广告内容", produces = MediaType.TEXT_HTML_VALUE)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "OK", response = RespModel.class),
            @ApiResponse(code = 500, message = "服务端异常", response = RespModel.class)})
//    @com.cxytiandi.encrypt.springboot.annotation.Decrypt
    @com.cxytiandi.encrypt.springboot.annotation.Encrypt
    public String queryBulletinById(@ApiParam(value = "广告ID") @PathParam("id") int id) {
        logger.info("通过广告ID查询h5=" + id);
        AD ad = new AD();
        ad.setId(id);
        AD result = adService.select(ad);
        String h5 ;
        if (result.getEditmode().equals("3")) {
            h5 = "<!DOCTYPE html><html><head><meta charset=\"utf-8\"></head>" + result.getContent() + "</html>";
        } else {
            h5 = "内容页不是h5链接，只有编辑模式是自编辑才使用此接口";
        }
        return h5;
    }

    @PostMapping(value = "/selectAll", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "传入type，返回广告列表数组", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "OK", response = RespModel.class),
            @ApiResponse(code = 500, message = "服务端异常", response = RespModel.class)})
    @com.cxytiandi.encrypt.springboot.annotation.Decrypt
    @com.cxytiandi.encrypt.springboot.annotation.Encrypt
    public RespModel<List<AD>> selectAll(@ApiParam(value = "入参type查询(1新闻内容公告页2新闻内容内容页3出行服务页4交警在线页);说明：editmode编辑模式1外链2内链3自定义编辑") @RequestBody AD adPage) {
        RespModel<List<AD>> resp = adService.selectAll(adPage);
        return resp;
    }

    /**
     * 获取开机广告列表
     *
     * @return
     */
    @PostMapping(value = "/getOpenAdvertList", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "OK", response = RespModel.class),
            @ApiResponse(code = 500, message = "服务端异常", response = RespModel.class)})
    @com.cxytiandi.encrypt.springboot.annotation.Decrypt
    @com.cxytiandi.encrypt.springboot.annotation.Encrypt
    public RespModel<List<Map<String, Object>>> getOpenAdvertList() {
        RespModel<List<Map<String, Object>>> resp = adService.getOpenAdvertList();
        return resp;
    }
}
